// pages/progressqryform/progressqryform.js

import config from '../../../utils/config.js'
import { HttpUtil, Target } from '../../../utils/http_util.js'
import { ServiceName, callService, registerService} from '../../../utils/store.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    usertype:0,
    userInfo:'',
    //表单对象
    formData: {
      id:'',
      name: '',
      sex:'1',
      age: '',
      idCard:'',
      phone: '',
      consumerPict:''
    },
    comsumerPictIcon: '',
    errormsg: {
      userNameEM: '',
      phoneEM: '',
      ageEM:'',
      idCardEM:''
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success(userres) {
        console.log(userres);
        that.setData({
           userInfo:userres.data,
          'formData.id': userres.data.id,
          'formData.name': userres.data.name,
          'formData.sex': userres.data.sex == '' ? '1':userres.data.sex+'',
          'formData.age': userres.data.age,
          'formData.idCard': userres.data.idCard,
          'formData.phone': userres.data.phone,
          'formData.consumerPict': userres.data.consumerPict,
        });
        if (userres.data.consumerPict==''){
          that.setData({
            comsumerPictIcon: "/images/default_tx.png"
          })
        }else{
          that.setData({
            comsumerPictIcon: config.ictx + userres.data.consumerPict
          })
        }
      }
    })
    this.setUploaderParam();
  },
  setUploaderParam:function(){
    const consumerPict = this.selectComponent('#consumerPict')
  },
  /**
   * 取消
   * 
   */
  cancel() {
    wx.navigateBack({
      delta: 1
    });
  },
  /**
   * 点击确定按钮
   * 
   */
  submit() {
    //提交
    var that = this;
    var data = that.data.formData;
    var errormsg = that.data.errormsg;
    var isPass = true;

    var uPattern = /^[\u4E00-\u9FA5A-Za-z0-9]+$/;
    if (!uPattern.test(data.name)) {
      // wx.showToast({
      //   title: '用户名格式不正确',
      //   icon: 'none',
      //   duration: 2000
      // })
      errormsg.userNameEM = '用户名格式不正确';
      isPass = false;
    }
    var uPhone = /^1(3|4|5|7|8)\d{9}$/;
    if (!uPhone.test(data.phone)) {
      // wx.showToast({
      //   title: '用机号码不正确',
      //   icon: 'none',
      //   duration: 2000
      // })
      errormsg.phoneEM = '手机号码不正确';

      isPass = false;
    }
    if (data.age==null||data.age==''){
      errormsg.ageEM = '请填写年龄';
      isPass = false;
    }
    var uIdCard = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
    if (!uIdCard.test(data.idCard)) {
      errormsg.idCardEM = '请填写正确身份证号';
      isPass = false;
    }
    if (!isPass) {
      that.setData({
        errormsg: errormsg
      })
      return false;
    }

    HttpUtil.get(Target.self, "bizConsumer/update", data).then(res => {
      if(res.status=='0000'){
        wx.showToast({
          title: '信息修改成功',
          icon: 'none',
          duration: 2000
        })
        wx.setStorageSync('userInfo', data)
        wx.reLaunch({
          url: '/pages/user/user-main/main'
        })
      }else{
        wx.showToast({
          title: '系统异常',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  /**
   * 绑定输入框 
   * 
  */
  onFieldChange(event) {
    var that =this;
    var em = that.data.errormsg;
    this.data.formData[event.currentTarget.id] = event.detail;
    em[event.currentTarget.id + 'EM'] = '';
    that.setData({
      errormsg:em
    })
  },
  /**
 * 选择框改变 
 * 
*/
  onSelectChange(event) {
    var that = this;
    that.setData({
      "formData.sex": event.detail
    });
  },
  /**

 * 上传图片

 */
  uploadimg: function () {
    var that = this;
    wx.chooseImage({  //从本地相册选择图片或使用相机拍照
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        //console.log(res)
        //前台显示
        that.setData({
          source: res.tempFilePaths
        })
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: config.ctx +'/upload/oss/image',
          filePath: tempFilePaths[0],
          name: 'file',
          header: { "content-type":"multipart/form-data"},
          formData:{
            id:that.data.formData.id,
            userType:0
          },
          success: function (res) {
            console.log(res);
            var resdata = JSON.parse( res.data ); 
            if (resdata.status =='0000'){
              that.setData({
                comsumerPictIcon: config.ictx + resdata.data,
                "formData.consumerPict": resdata.data
              })
            }else{
              wx.showToast({
                title: '头像上传异常',
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  }
})